********************************************************************************
* Misattributed Blame? Attitudes Toward Globalization in the Age of Automation 
* Nicole Wu
* Political Science Research and Methods (PSRM)
* Data files: productivity.dta, automation.dta, anes2016_cdata_518.dta
********************************************************************************		

*** FIGURE 1
use "productivity.dta"
gen dateq = tq(1987q2) + datem - 1
tsset dateq, quarterly
graph twoway line outms ophmfg manemp_nbd20090101 dateq, ytitle("Index 2009=100") xtitle("Year") legend(symxsize(8) forcesize) tlabel(1988q1 1998q1 2008q1 2018q1, tpos(in)) scheme(plottigblind) legend(pos(5) col(2))
***

*** FIGURE 2
use "automation.dta", clear
graph hbar automation, over(occupation_onet, sort(automation) descending) over(plot) title("Level of Automation Reported by Occuaptional Experts of Job Incumbents", span) ysize(7) xsize(15)
***

*** FIGURE 3
use "anes2016_cdata_0518.dta", clear
scatter auto_onetavg auto_fbavg, ysca(alt) xsca(alt) xlabel(, grid gmax) saving(main)
twoway histogram auto_fbavg, density xsca(alt reverse) horiz xlabel(none) saving(y) fxsize(25)
twoway histogram auto_onetavg, density ysca(alt reverse) ylabel(none, grid gmax) xlabel(, grid gmax) saving(x) fysize(25)              
graph combine y.gph main.gph x.gph, hole(3) imargin(0 0 0 0) graphregion(margin(l=22 r=22))

*** TABLE 2 MAIN MODEL
eststo clear
eststo: meologit immglevel auto_fbavg auto_onetavg if laborforce == 1 || cc_onet: || industry: || cd_merge: 
eststo: meologit immglevel auto_fbavg auto_onetavg offshore_avg imppen2012 foreignbornpc100 gender partyid age edu nationalism ethnocentric100 famincome if laborforce == 1 || cc_onet: || industry: || cd_merge: 
eststo: meologit opposetrade auto_fbavg auto_onetavg if laborforce == 1 || cc_onet: || industry: || cd_merge: 
eststo: meologit opposetrade auto_fbavg auto_onetavg offshore_avg imppen2012 foreignbornpc100 gender partyid age edu nationalism ethnocentric100 famincome if laborforce == 1 || cc_onet: || industry: || cd_merge: 
eststo: meologit outsource auto_fbavg auto_onetavg if laborforce == 1 || cc_onet: || industry: || cd_merge: 
eststo: meologit outsource auto_fbavg auto_onetavg offshore_avg imppen2012 foreignbornpc100 gender partyid age edu nationalism ethnocentric100 famincome if laborforce == 1 || cc_onet: || industry: || cd_merge: 
*esttab using mainresults_518.tex, b(2) se(2) label replace booktabs alignment(D{.}{.}{-1}) title(Regression table\label{tab1}) page(dcolumn) nonumber 

*** FIGURE 4
ologit immglevel3 auto_fbavg auto_onetavg offshore_avg imppen2012 foreignbornpc100 gender partyid age edu nationalism ethnocentric100 famincome if laborforce == 1 
margins, dydx(auto_fbavg auto_onetavg offshore_avg imppen2012 foreignbornpc100 gender partyid age edu nationalism ethnocentric100 famincome) predict(outcome(2)) post
est store A
ologit opposetrade3 auto_fbavg auto_onetavg offshore_avg imppen2012 foreignbornpc100 gender partyid age edu nationalism ethnocentric100 famincome if laborforce == 1 
margins, dydx(auto_fbavg auto_onetavg offshore_avg imppen2012 foreignbornpc100 gender partyid age edu nationalism ethnocentric100 famincome) predict(outcome(2)) post
est store B
ologit outsource2 auto_fbavg auto_onetavg offshore_avg imppen2012 foreignbornpc100 gender partyid age edu nationalism ethnocentric100 famincome if laborforce == 1 
margins, dydx(auto_fbavg auto_onetavg offshore_avg imppen2012 foreignbornpc100 gender partyid age edu nationalism ethnocentric100 famincome) predict(outcome(2)) post
est store C

coefplot (A, label(Fewer immigrants)), drop(_cons imppen2012 famincome offshore_avg) xline(0) levels(95) xtitle(Average Marginal Effects - Fewer immigrants)
graph save Graph "A.gph", replace
coefplot (B, label(Oppose trade) msymbol(T)), drop(_cons imppen2012 famincome offshore_avg) xline(0) levels(95) xtitle(Average Marginal Effects - Oppose trade)
graph save Graph "B.gph", replace
coefplot (C, label(Discourage offshoring) msymbol(S)), drop(_cons imppen2012 famincome offshore_avg) xline(0) levels(95) xtitle(Average Marginal Effects - Discourage offshoring) 
graph save Graph "C.gph", replace
*graph combine "A.gph" "B.gph" "C.gph"

*** TABLE 3
eststo clear
eststo: ologit opposetrade auto_onetavg foreignbornpc100 gender partyid age edu nationalism ethnocentric100 if laborforce == 0 | unemployed == 1
eststo: ologit immglevel auto_onetavg foreignbornpc100 gender partyid age edu nationalism ethnocentric100 if laborforce == 0 | unemployed == 1
eststo: ologit outsource auto_onetavg foreignbornpc100 gender partyid age edu nationalism ethnocentric100 if laborforce == 0 | unemployed == 1
esttab using displaced.tex, b(2) se(2) label replace booktabs alignment(D{.}{.}{-1}) title(Regression table\label{tab1}) page(dcolumn) nonumber 

*** TABLE 4 NONTRADABLE
eststo clear
eststo: ologit immglevel auto_fbavg auto_onetavg if laborforce == 1 & offshore_avg==1
eststo: ologit immglevel auto_fbavg auto_onetavg imppen2012 foreignbornpc100 gender partyid age edu nationalism ethnocentric100 famincome if laborforce == 1 & offshore_avg==1
eststo: ologit opposetrade auto_fbavg auto_onetavg if laborforce == 1 & offshore_avg==1
eststo: ologit opposetrade auto_fbavg auto_onetavg imppen2012 foreignbornpc100 gender partyid age edu nationalism ethnocentric100 famincome if laborforce == 1 & offshore_avg==1
eststo: ologit outsource auto_fbavg auto_onetavg if laborforce == 1 & offshore_avg==1
eststo: ologit outsource auto_fbavg auto_onetavg imppen2012 foreignbornpc100 gender partyid age edu nationalism ethnocentric100 famincome if laborforce == 1 & offshore_avg==1
esttab using nontradable.tex, b(2) se(2) label replace booktabs alignment(D{.}{.}{-1}) title(Attitudes toward globalization and tech spending, non-tradable sectors\label{tab1}) page(dcolumn) nonumber 

*** FIGURE 5 (FULL AND NONTRADABLE SUBSAMPLE)
eststo clear
ologit immglevel3 auto_fbavg auto_onetavg imppen2012 foreignbornpc100 gender partyid age edu nationalism ethnocentric100 famincome if laborforce == 1 & offshore_avg==1
margins, dydx(auto_fbavg auto_onetavg imppen2012 foreignbornpc100 gender partyid age edu nationalism ethnocentric100 famincome) predict(outcome(2)) post
est store E
ologit opposetrade3 auto_fbavg auto_onetavg imppen2012 foreignbornpc100 gender partyid age edu nationalism ethnocentric100 famincome if laborforce == 1 & offshore_avg==1
margins, dydx(auto_fbavg auto_onetavg imppen2012 foreignbornpc100 gender partyid age edu nationalism ethnocentric100 famincome) predict(outcome(2)) post
est store F
ologit outsource2 auto_fbavg auto_onetavg imppen2012 foreignbornpc100 gender partyid age edu nationalism ethnocentric100 famincome if laborforce == 1 & offshore_avg==1
margins, dydx(auto_fbavg auto_onetavg imppen2012 foreignbornpc100 gender partyid age edu nationalism ethnocentric100 famincome) predict(outcome(2)) post
est store G

coefplot (A, label(Main sample)) (E, label(Nontradable subsample) msymbol(D)), drop(_cons imppen2012 famincome offshore_avg) xline(0) levels(95) xtitle(Average Marginal Effects - Fewer immigrants) 
graph save Graph "E.gph", replace
coefplot (B, label(Main sample)) (F, label(Nontradable subsample) msymbol(D)), drop(_cons imppen2012 famincome offshore_avg) xline(0) levels(95) xtitle(Average Marginal Effects - Oppose trade) 
graph save Graph "F.gph", replace
coefplot (C, label(Main sample)) (G, label(Nontradable subsample) msymbol(D)), drop(_cons imppen2012 famincome offshore_avg) xline(0) levels(95) xtitle(Average Marginal Effects - Discourage offshoring)
graph save Graph "G.gph", replace
*graph combine "E.gph" "F.gph" "G.gph", cols(1)

*** SUPPLEMENTARY MATERIALS (BASED ON ANES2016_IO_CDATA.DTA)
*** WITH WEIGHTS
*** TABLE 5
svyset [pweight= V160102], strata(V160201) psu(V160202)
eststo clear
eststo: svy: ologit immglevel auto_fbavg auto_onetavg if laborforce == 1
eststo: svy: ologit immglevel auto_fbavg auto_onetavg offshore_avg imppen2012 foreignbornpc100 gender partyid age edu nationalism ethnocentric100 famincome  if laborforce == 1
eststo: svy: ologit opposetrade auto_fbavg auto_onetavg if laborforce == 1
eststo: svy: ologit opposetrade auto_fbavg auto_onetavg offshore_avg imppen2012 foreignbornpc100 gender partyid age edu nationalism ethnocentric100 famincome if laborforce == 1
eststo: svy: ologit outsource auto_fbavg auto_onetavg if laborforce == 1
eststo: svy: ologit outsource auto_fbavg auto_onetavg offshore_avg imppen2012 foreignbornpc100 gender partyid age edu nationalism ethnocentric100 famincome if laborforce == 1
*esttab using appendix1_518.tex, b(2) se(2) label replace booktabs alignment(D{.}{.}{-1}) title(Attitudes toward globalization, entire labor force, with weights \label{tab1}) page(dcolumn) nonumber 

*** TABLE 6
eststo clear
eststo: svy: ologit decreasestemspend auto_fbavg auto_onetavg if laborforce == 1 
eststo: svy: ologit decreasestemspend auto_fbavg auto_onetavg gender partyid age edu famincome if laborforce == 1 
eststo: svy: ologit decreasestemspend auto_fbavg auto_onetavg offshore_avg imppen2012 foreignbornpc100 gender partyid age edu nationalism ethnocentric100 famincome if laborforce == 1 
*esttab using appendix1b.tex, b(2) se(2) label replace booktabs alignment(D{.}{.}{-1}) title(Attitudes toward tech spending, entire labor force, with weights \label{tab1}) page(dcolumn) nonumber 

*** TABLE 7
*** SUBSAMPLE ANALYSIS: POTENTIALLY DISPLACED INDIVIDUALS, EXCLUDING RETIREES
eststo clear
eststo: ologit opposetrade auto_onetavg foreignbornpc100 gender partyid age edu nationalism ethnocentric100 if retired!=1 & laborforce == 0 | unemployed == 1 
eststo: ologit immglevel auto_onetavg foreignbornpc100 gender partyid age edu nationalism ethnocentric100 if retired!=1 & laborforce == 0 | unemployed == 1 
eststo: ologit outsource auto_onetavg foreignbornpc100 gender partyid age edu nationalism ethnocentric100 if retired!=1 & laborforce == 0 | unemployed == 1 
eststo: ologit decreasestemspend auto_onetavg foreignbornpc100 gender partyid age edu nationalism ethnocentric100 if retired!=1 & laborforce == 0 | unemployed == 1 
esttab using displaced_notretired.tex, b(2) se(2) label replace booktabs alignment(D{.}{.}{-1}) title(Attitudes toward globalization and technology, potentially displaced individuals\label{tab1}) page(dcolumn) nonumber 

*** A.4.1
corrtable auto_fbavg auto_onetavg offshore_avg imppen2012 foreignbornpc100 gender partyid age edu nationalism ethnocentric famincome if laborforce == 1, half

*** A.4.2
collin auto_fbavg auto_onetavg offshore_avg imppen2012 foreignbornpc100 gender partyid age edu nationalism ethnocentric famincome if laborforce == 1

*** A.5
corrtable immglevel opposetrade outsource if laborforce == 1, half
label define opposetrade3 0 "Favor" 1 "Neither favor nor oppose" 2 "Oppose" 
label values opposetrade3 opposetrade3
vioplot immglevel, over(opposetrade3) ytitle("Decrease(5) or Increase(1) Immigration") xtitle("Trade")
label define outsource2 0 "Encourage" 1 "Should stay out of this matter" 2 "Discourage" 
label values outsource2 outsource2
vioplot immglevel, over(outsource2) ytitle("Decrease(5) or Increase(1) Immigration") xtitle("Offshoring")
vioplot outsource2, over(opposetrade3) ytitle("Encrouage(1) or Discourage(3) Offshoring") xtitle("Trade")
 
*** A.6
*** FIGURE 6
logit jobworry auto_fbavg if laborforce==1
margins, at(auto_fbavg=(0 .10 .20 .30 .40 .50 .60 .70 .80 .90 1)) vsquish
marginsplot, recast(line) recastci(rarea) ci1opts(fintensity(20)) 

*** A.7
*** TABLE 9
eststo clear
eststo: meologit decreasestemspend auto_fbavg auto_onetavg if laborforce == 1 || cc_onet: || industry: || cd_merge: 
eststo: meologit decreasestemspend auto_fbavg auto_onetavg gender partyid age edu famincome if laborforce == 1 || cc_onet: || industry: || cd_merge: 
eststo: meologit decreasestemspend auto_fbavg auto_onetavg offshore_avg imppen2012 foreignbornpc100 gender partyid age edu nationalism ethnocentric famincome if laborforce == 1 || cc_onet: || industry: || cd_merge: 
*esttab using appendixt9.tex, b(2) se(2) label replace booktabs alignment(D{.}{.}{-1}) title(Attitudes toward tech spending (workers in labor force) \label{tab1}) page(dcolumn) nonumber 



